<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="cancle"
    >
      <el-form ref="dict" :model="addParams" :rules="rules" label-width="80px">
        <template v-if="title === '新增字典' || title === '编辑字典'">
          <el-form-item label="字典名称" prop="name">
            <el-input v-model="addParams.name"></el-input>
          </el-form-item>
          <el-form-item label="字典编码" prop="code">
            <el-input v-model="addParams.code"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input
              v-model="addParams.remark"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
            >
            </el-input>
          </el-form-item>
        </template>
        <template v-if="title === '新增字典项' || title === '编辑字典项'">
          <el-form-item label="字典编码" prop="code">
            <el-input v-model="addParams.code" disabled></el-input>
          </el-form-item>
          <el-form-item label="名称" prop="name">
            <el-input v-model="addParams.name"></el-input>
          </el-form-item>
          <el-form-item label="值" prop="value">
            <el-input v-model="addParams.value"></el-input>
          </el-form-item>
          <el-form-item label="排序" prop="orderNum">
            <el-input-number v-model="addParams.orderNum" controls-position="right" :min="0"></el-input-number>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="addParams.remark"
              type="textarea"
              :rows="2"
              placeholder="请输入内容"
            >
            </el-input>
          </el-form-item>
        </template>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateDict">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'AddOrUpdateDict',
  props: {
    addParams: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dialogVisible: false,
      title: '',
      rules: {
        name: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入字典编码', trigger: 'blur' }],
        value: [{ required: true, message: '请输入字典值', trigger: 'blur' }]
      }
    }
  },
  methods: {
    addOrUpdateDict() {
      this.$emit('addOrUpdateDict')
      try {
        this.$refs['dict'].resetFields()
      } catch (error) {}
      this.dialogVisible = false
    },
    cancle() {
      try {
        this.$refs['dict'].resetFields()
      } catch (error) {}
      this.dialogVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
